var twbStoreMembervm = new Vue({
    el: '#app',
    data: {
        tableData: [],//表格当前页数据
        pagesize: 20,//默认每页数据量
        storeMember: {}, //当前选择行数据
        currentPage: 1,//当前页码
        totalCount: 0,//默认数据总数
        multipleSelection: [],//多选数组
        q: {//搜索条件
            username: '',
            hangsAccountCustomer: '',//挂账客户
            merchantsSource: ''//会员类型
        },
        dialogFormVisible: false,
        title: '',
        storeFavouredPriceEntityList: [],//价格账套
        labelPosition: 'right',
        allCount:'',
        mallCount:'',
        twbCount:''
    },
    created: function () {
        Vue.nextTick(function () {
            //载入数据
            twbStoreMembervm.loadData(twbStoreMembervm.currentPage, twbStoreMembervm.pagesize);
        });
    },
    methods: {
        //从后台获取数据
        loadData: function (page, limit) {
            var params = {
                page: page,
                limit: limit,
                username: twbStoreMembervm.q.username,
                hangsAccountCustomer: twbStoreMembervm.q.hangsAccountCustomer,
                merchantsSource: twbStoreMembervm.q.merchantsSource + ''
            }
            $.ajax({
                type: "POST",
                url: baseURL + 'storeMember/storemember/twblist',
                contentType: "application/json",
                data: JSON.stringify(params),
                success: function (r) {
                    if (r.code === 0) {
                        twbStoreMembervm.tableData = r.page.list
                        twbStoreMembervm.totalCount = r.page.totalCount
                        twbStoreMembervm.storeFavouredPriceEntityList = r.storeFavouredPriceEntityList
                        twbStoreMembervm.allCount=r.allCount
                        twbStoreMembervm.mallCount=r.mallCount
                        twbStoreMembervm.twbCount=r.twbCount
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        //多选响应
        handleSelectionChange: function (val) {
            // twbStoreMembervm.multipleSelection = val;
            // console.log(twbStoreMembervm.multipleSelection)
        },
        //点击行响应
        handleclick: function (row, event, column) {
            // twbStoreMembervm.storeMember = JSON.parse(JSON.stringify(row));//深克隆
        },
        //弹出详情框
        showDetail: function (row) {
            twbStoreMembervm.title = "用户详情";
            twbStoreMembervm.storeMember = row
            twbStoreMembervm.dialogFormVisible = true
        },
        //弹出新增框
        add: function () {
            twbStoreMembervm.title = "新增用户";
            twbStoreMembervm.dialogFormVisible = true
            twbStoreMembervm.labelPosition='right'
            twbStoreMembervm.storeMember = {hangsAccountCustomer: 0};
        },
        //弹出编辑框
        update: function (index, row) {
            twbStoreMembervm.title = '编辑用户'
            twbStoreMembervm.storeMember = JSON.parse(JSON.stringify(row));//深克隆
            twbStoreMembervm.dialogFormVisible = true
        },
        saveOrUpdate: function (formName) {
            this.$refs[formName].validate(function (valid) {
                if (valid) {
                    if (!(/^[1][3,4,5,6,7,8,9][0-9]{9}$/.test(twbStoreMembervm.storeMember.mobile))) {
                        alert("手机号码格式不正确");
                        return false;
                    }
                    var url = twbStoreMembervm.storeMember.sid == null ? "storeMember/storemember/twbsave" : "storeMember/storemember/update";
                    $.ajax({
                        type: "POST",
                        url: baseURL + url,
                        contentType: "application/json",
                        data: JSON.stringify(twbStoreMembervm.storeMember),
                        success: function (r) {
                            if (r.code === 0) {
                                twbStoreMembervm.$alert('操作成功!', '提示', {
                                    confirmButtonText: '确定',
                                    callback: function (action) {
                                        twbStoreMembervm.loadData(twbStoreMembervm.currentPage = 1, twbStoreMembervm.pagesize);//刷新后回到首页
                                        twbStoreMembervm.$refs[formName].resetFields();
                                        twbStoreMembervm.dialogFormVisible = false
                                    }
                                });
                            } else {
                                alert(r.msg);
                            }
                        }
                    });
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        //删除恢复
        del: function (index, row) {
            twbStoreMembervm.$alert(row.del === 0 ? '确定删除?' : '确定恢复?', '提示', {
                confirmButtonText: '确定',
                callback: function (action) {
                    $.ajax({
                        type: "POST",
                        url: "userinfo/delete",
                        contentType: "application/json",
                        data: JSON.stringify(row),
                        success: function (r) {
                            if (r.code === 0) {
                                twbStoreMembervm.loadData(twbStoreMembervm.currentPage, twbStoreMembervm.pagesize);//刷新后页面在当前页
                                //twbStoreMembervm.loadData(twbStoreMembervm.currentPage=1, twbStoreMembervm.pagesize);刷新后页面回到首页
                            } else {
                                alert(r.msg);
                            }
                        }
                    });
                }
            });
        },
        //搜索
        search: function () {
            twbStoreMembervm.loadData(twbStoreMembervm.currentPage, twbStoreMembervm.pagesize);
        },
        //重置
        rest: function () {
            twbStoreMembervm.q.username = ''
            twbStoreMembervm.q.hangsAccountCustomer = ''
            twbStoreMembervm.q.merchantsSource = ''
            twbStoreMembervm.loadData(twbStoreMembervm.currentPage = 1, twbStoreMembervm.pagesize);
        },
        //每页显示数据量变更
        handleSizeChange: function (val) {
            twbStoreMembervm.pagesize = val;
            twbStoreMembervm.loadData(twbStoreMembervm.currentPage, twbStoreMembervm.pagesize);
        },
        //页码变更
        handleCurrentChange: function (val) {
            twbStoreMembervm.currentPage = val;
            twbStoreMembervm.loadData(twbStoreMembervm.currentPage, twbStoreMembervm.pagesize);
        },
        merchantsSourceFormatter: function (val) {
            if (val.merchantsSource == 0) {
                return '商城'
            }
            else if (val.merchantsSource == 1) {
                return '图文宝'
            }
            else {
                return ''
            }
        },
        hangsAccountCustomerFormatter: function (val) {
            if (val.hangsAccountCustomer == 0) {
                return '否'
            }
            else if (val.hangsAccountCustomer == 1) {
                return '是'
            }
            else {
                return ''
            }
        },

        accountCustomer: function () {
            twbStoreMembervm.q.hangsAccountCustomer = 1
            twbStoreMembervm.q.merchantsSource = ''
            twbStoreMembervm.search()
        },
        twbCustomer: function () {
            twbStoreMembervm.q.merchantsSource = 1
            twbStoreMembervm.q.hangsAccountCustomer = ''
            twbStoreMembervm.search()
        },
        mallCustomer: function () {
            twbStoreMembervm.q.merchantsSource = '0'
            twbStoreMembervm.q.hangsAccountCustomer = ''
            twbStoreMembervm.search()
        },
    }
});
